﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.GetAction("ETL", "GetDataSourceList", "1.0")]
    public class GetDataSourceListAction : BaseAction<GetDataSourceListParams, GetDataSourceListResult>
    {
        protected override ActionResult<GetDataSourceListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataSourceListParams opParams)
        {
            GetDataSourceListResult result = new GetDataSourceListResult();

            SER.DtSerService ser = new SER.DtSerService();

            PagedList<VIW.VDtSource> serResult = ser.GetDtSourceList(opParams.DataServiceId, null, opParams.Page * 25, 25);

            if (serResult.IsSuccess)
            {
                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Total = serResult.Total;

                foreach (var model in serResult.Data)
                {
                    GetDataSourceListResult.Item item = new GetDataSourceListResult.Item();
                    item.Id = model.Id;
                    item.Name = model.Name;
                    if(!string.IsNullOrEmpty(model.MappingTable))
                    {
                        item.MappingTable = model.MappingTable;
                    }
                    else
                    {
                        item.MappingTable = string.Empty;
                    }
                    if (model.MappingTableId!=null)
                    {
                        item.MappingTableId = model.MappingTableId.Value;
                    }

                    item.Type = model.Type;
                    if(!string.IsNullOrEmpty(model.Value))
                    {
                        item.Value = model.Value;
                    }
                    else
                    {
                        item.Value = string.Empty;
                    }

                    result.Items.Add(item);
                }

            return new ActionResult<GetDataSourceListResult>(result);

            }
            return new ActionResult<GetDataSourceListResult>(false, serResult.ErrorCode, serResult.Message, null);
        }
    }

    public class GetDataSourceListParams
    {
        [Parameter]
        public Guid DataServiceId { get; set; }

        [Parameter]
        public int Page { get; set; }

        public string HasTask { get; set; }
    }

    public class GetDataSourceListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetDataSourceListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string MappingTable { get; set; }
            public Guid MappingTableId { get; set; }

            public int Type { get; set; }

            public string Value { get; set; }
        }
    }
}
